package org.footballmanager.pandafootball.system.controller;

import org.footballmanager.pandafootball.common.constants.Constants;
import org.footballmanager.pandafootball.system.domain.FootPerson;
import org.footballmanager.pandafootball.system.domain.FootUser;
import org.footballmanager.pandafootball.system.domain.enums.QX;
import org.footballmanager.pandafootball.system.form.impl.FootUserSearchForm;
import org.footballmanager.pandafootball.system.service.FootUserPersonService;
import org.footballmanager.pandafootball.system.service.FootUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.support.SessionStatus;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@SessionAttributes("footUserSearchForm")
@RequestMapping("/foot/footUser")
public class FootUserController extends AddAndEditController<FootUser> {
    /* 实体名称 */
    private static final String ENTITYNAME = Constants.USER;
    private static final String EDITPAGE = String.format(Constants.EDITPAGE, Constants.FOOT, ENTITYNAME);
    private static final String LISTPAGE = String.format(Constants.LISTPAGE, Constants.FOOT, ENTITYNAME);
    private static final String VIEWPAGE = String.format(Constants.VIEWPAGE, Constants.FOOT, ENTITYNAME);
    private static final String REDIRECT = String.format(Constants.REDIRECT, Constants.FOOT, ENTITYNAME);

    @Autowired
    private FootUserService footUserService;
    @Autowired
    private FootUserPersonService footUserPersonService;

    /**
     * 抽象方法
     *
     * @return 实体名称
     */
    @Override
    protected String getEntityName() {
        return ENTITYNAME;
    }

    /**
     * 初始化
     *
     * @param map                参数
     * @param footUserSearchForm 条件参数
     */
    @ModelAttribute
    public void init(Model map, @ModelAttribute FootUserSearchForm footUserSearchForm) {
        FootUserSearchForm newFootUserSearchForm = new FootUserSearchForm();
        if (footUserSearchForm != null) {
            newFootUserSearchForm = footUserSearchForm;
        }
        map.addAttribute(String.format(Constants.FORMNAME, ENTITYNAME), newFootUserSearchForm);
    }

    @RequestMapping(value = "/search")
    public String search(Model map, @ModelAttribute FootUserSearchForm footUserSearchForm) {
        footUserService.search(ENTITYNAME, map, footUserSearchForm);
        return LISTPAGE;
    }

    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    public String delete(@RequestParam Long id, HttpServletRequest request) {
        footUserService.delete(id, request);
        return REDIRECT;
    }

    @RequestMapping(value = "/deleteall", method = RequestMethod.POST)
    public String deleteall(HttpServletRequest request, @ModelAttribute FootUserSearchForm footUserSearchForm) {
        footUserService.deleteAll(footUserSearchForm, request);
        return REDIRECT;
    }

    @RequestMapping(value = "/edit", method = RequestMethod.GET)
    public String openEditPage(Model map, @RequestParam Long id) {
        map.addAttribute(ENTITYNAME, footUserService.get(id));
        return EDITPAGE;
    }

    @RequestMapping(value = "/editVip", method = RequestMethod.GET)
    public String openEditVipPage(Model map, @RequestParam Long id) {
        map.addAttribute(ENTITYNAME, footUserService.get(id));
        return "foot/footUser/EditVip";
    }

    @RequestMapping(value = "/editVip", method = RequestMethod.POST)
    public String updateVip(@RequestParam Long id, @RequestParam QX qx, Model map,
                            HttpServletRequest request, SessionStatus s) {
        if (id == null || qx == null) {
            map.addAttribute(Constants.MSG, "请选择权限！");
            return "foot/footUser/EditVip";
        } else {
            footUserService.changeUser2Vip(id, qx, request);
            s.setComplete();
            return REDIRECT;
        }
    }

    @RequestMapping(value = "/view", method = RequestMethod.GET)
    public String openViewPage(Model map, @RequestParam Long id, @ModelAttribute FootUserSearchForm footUserSearchForm,
                               HttpServletRequest request) {
        FootUser user = footUserService.get(id);
        map.addAttribute(String.format(Constants.FORMNAME, ENTITYNAME), footUserSearchForm);
        map.addAttribute(ENTITYNAME, user);
        List<FootPerson> list = footUserPersonService.findFootPersonByUserId(user.getId(), request);
        if (null != list && !list.isEmpty()) {
            double sum = 0D;
            for (FootPerson json : list) {
                sum += Double.valueOf(json.getQysj());
            }
            map.addAttribute("sum", sum);
            map.addAttribute("qyList", list);
        }
        map.addAttribute("power", footUserPersonService.getPower(user.getId(), request));
        return VIEWPAGE;
    }

}
